<?php
/* QQ:34734852
 * 业务联系：小鱼
 */
class TaoBaoIPAPI{
 private $ApiUrl  = 'http://whois.pconline.com.cn/ip.jsp?ip='; //太平洋ip库接口地址
 private $c       = object;
 private $ip, $add = '保留地址/未知地址';

 public function __construct( $curl = object ) {
	 $this -> c = $curl;
	 unset( $curl );
 }
 
 private function Request( $ip = '', $name = '' ) {
	 if ( empty( $ip ) && $name != 'add' ) {
		 $this -> GetIP();
	 } else {
		 $d = $this -> c -> post($this -> ApiUrl  . $this->GetIP( $ip ), false );
		 if ( $d  ) {
		   $this -> add = mb_convert_encoding($d, 'UTF-8', 'GBK');
		 } 
		 unset( $d );
	 }
 }
 
 private function GetIP( $ip = '' ){ 
    if ( !empty( $ip ) && preg_match('/^(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])\.(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])\.(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])\.(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])$/is', $ip ) ) {
		$this -> ip = $ip;
		return $ip;
	} else {
		if (getenv('HTTP_CLIENT_IP') && strcasecmp(getenv('HTTP_CLIENT_IP'), 'unknown'))  {
		  $ip = getenv('HTTP_CLIENT_IP'); 
		} elseif (getenv('HTTP_X_FORWARDED_FOR') && strcasecmp(getenv('HTTP_X_FORWARDED_FOR'), 'unknown')) {
		  $ip = getenv('HTTP_X_FORWARDED_FOR'); 
		} elseif (getenv('REMOTE_ADDR') && strcasecmp(getenv('REMOTE_ADDR'), 'unknown')) {
		  $ip = getenv('REMOTE_ADDR'); 
		} elseif (isset($_SERVER['REMOTE_ADDR']) && $_SERVER['REMOTE_ADDR'] && strcasecmp($_SERVER['REMOTE_ADDR'], 'unknown')) {
		  $ip = $_SERVER['REMOTE_ADDR']; 
		} else  {
		  $ip = 'unknown'; 
		}
		$ip != '::1' or $ip = '127.0.0.1';
		$this -> ip = $ip;
		return $ip; 
	}
  } 
 
 
 public function __get( $Name ) {
	 return $this -> hq( $Name );
 }
 
 public function get( $t = 'ip', $ip = '127.0.0.1' ) {
	 return  $this -> hq( $t, $ip );
 }
 
 private function  hq( $name, $ip = '' ) {
	 $this -> Request( $ip, $name );
	 switch( $name ) {
		 case 'ip': return $this -> ip; break;
		 default:   return $this -> add;
	 }
 }
 
 
}